import utils from '../../../lib/StringExtend'


const GetAddress = {

  getUserAddress(context, {vm: vm}) {
    context.dispatch('getShopWxConfig', {vm: vm});
  },
  //微信配置项
  getShopWxConfig(context, {vm: vm}) {
    let params = {
      current_url: window.location.href,
      wechat_id: utils.getCookie('wechat_id')
    };
    vm.$axios.getJssdkSign(params).then(res => {
      if (res.errcode !== '000') {
        vm.$dialog.alert({
          message: res.errmsg,
        });
        return false;
      }
      wx.config(res.data);
      wx.ready(() => {
        //获取当前经纬度
        context.dispatch('getLocation', {vm: vm});
        wx.error((res) => {
          console.log(res)
        });
      })
    });
  },
  //获取当前经纬度
  getLocation(context, {vm: vm}) {
    wx.getLocation({
      success: (res) => {
        let obj = {
          longitude: res.longitude,
          latitude: res.latitude
        };
        context.commit('userAddress', obj);
      },
      cancel: (res) => {
        // alert('用户拒绝授权获取地理位置');
        context.dispatch('getLocation', {vm: vm});
      }
    });
  },
  //高德逆地理编码   参数（经度;维度）
  escapeAddress(context, {vm: vm, longitude: longitude, latitude: latitude}) {
    let params = {
      key: 'e2a07cae1f596a7320af2033c06cef67',
      location: longitude + ',' + latitude
    };
    vm.$axios.amap(params).then(res => {
      context.commit('userAddress', res.data.regeocode);
    })
  }
};
export default GetAddress
